//
//  DataService.m
//  one·一个  tyw
//
//  Created by tyw on 15/9/15.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "DataService.h"
#import "AFNetworking.h"

@implementation DataService

+ (void)oneRequestUrl:(NSString *)url httpMethod:(NSString *)method params:(NSMutableDictionary *)params block:(DataBlockType)block {
    //01 构造url
    NSString* fullUrl = [kBaseUrl stringByAppendingString:url];
    
    //02 manager
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    //设置响应数据的解析格式
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    //03 发送请求
    if ([method isEqualToString:@"GET"]) {
        [manager GET:fullUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            block(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"网络GET请求失败  %@", error);
            block(error);
        }];
        
    } else if ([method isEqualToString:@"POST"]) {
        [manager POST:fullUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            block(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"网络POST请求失败  %@", error);
            block(error);
        }];
        
    } else {
        NSLog(@"请求方式错误");
        return;
    }
}

@end
